package com.lzy.managersystem.controller;

import cn.hutool.core.date.DateUtil;
import com.lzy.managersystem.pojo.Comment;
import com.lzy.managersystem.service.CommentService;
import com.lzy.managersystem.tools.Result;
import com.lzy.managersystem.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/getCommentUser/{articleId}")
    public Result getCommentUser(@PathVariable("articleId") Integer articleId) {
        //查询所有评论
        List<Comment> comment = commentService.getComment(articleId);
        //查询子评论
        List<Comment> collect = comment.stream().filter(comment1 -> comment1.getOriginId() == null).collect(Collectors.toList());

        //设置评论数据的子节点
        for (Comment origin : collect) {
            List<Comment> collect1 = comment.stream().filter(comment1 -> origin.getId().equals(comment1.getOriginId())).collect(Collectors.toList()); //表示回复对象集合
            collect1.forEach(comment2 -> {
                comment.stream().filter(c1 -> c1.getId().equals(comment2.getPid())).findFirst().ifPresent(v -> {  //找到父级评论用户id和用户昵称，并设置当前回复对象
                    comment2.setPUserId(v.getUserId());
                    comment2.setPNickname(v.getNickname());
                });
            });
            origin.setChildren(collect1);
        }
        collect.forEach(c -> {
            System.out.println(c);
        });
        return Result.success(collect);
    }


    @PostMapping("/inster")
    public Result insertUser(@RequestBody Comment comment) {
        comment.setTime(DateUtil.now());
        comment.setUserId(TokenUtils.getCurrentUser().getId());
        Integer pid = comment.getPid();
        Comment pComment = commentService.getById(pid);
        if (pComment.getOriginId() != null) { //如果当前父级回复有祖宗，那么就设置相同的祖宗
            comment.setOriginId(pComment.getOriginId());
        } else {//否则设置父级为当前祖宗
//            comment.setPid(comment.getId());
            comment.setOriginId(comment.getPid());
        }
        int flag = commentService.insterUser(comment);
        return Result.success(flag);
    }

    @PostMapping("/insterzhu")
    public Result insterzhupinglun(@RequestBody Comment comment) {
        comment.setTime(DateUtil.now());
        comment.setUserId(TokenUtils.getCurrentUser().getId());

        int flag = commentService.insterUser(comment);
        return Result.success(flag);
    }


    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        int flag = commentService.deleteUser(id);
        return Result.success(flag);
    }

    @PutMapping("/")
    public Result UpdataUser(@RequestBody Comment comment) {
        comment.setTime(DateUtil.now());
        comment.setUserId(TokenUtils.getCurrentUser().getId());
        int flag = commentService.updateUser(comment);
        return Result.success(flag);
    }

}
